package com.spic.business.plan.team.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.business.plan.team.domain.PlanPost;
import com.spic.business.plan.team.service.IPlanPostService;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.page.TableDataInfo;

/**
 * 应急岗位Controller
 * 
 * @author spic
 * @date 2021-11-10
 */
@RestController
@RequestMapping("/plan/planPost")
public class PlanPostController extends BaseController
{
    @Autowired
    private IPlanPostService planPostService;

    /**
     * 查询应急岗位列表
     */
    @PreAuthorize(hasPermi = "business/plan:planPost:list")
    @GetMapping("/list")
    public TableDataInfo list(PlanPost planPost)
    {
        startPage();
        List<PlanPost> list = planPostService.selectPlanPostList(planPost);
        return getDataTable(list);
    }

    /**
     * 导出应急岗位列表
     */
    @PreAuthorize(hasPermi = "business/plan:planPost:export")
    @Log(title = "应急岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlanPost planPost) throws IOException
    {
        List<PlanPost> list = planPostService.selectPlanPostList(planPost);
        ExcelUtil<PlanPost> util = new ExcelUtil<PlanPost>(PlanPost.class);
        util.exportExcel(response, list, "应急岗位数据");
    }

    /**
     * 获取应急岗位详细信息
     */
    @PreAuthorize(hasPermi = "business/plan:planPost:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(planPostService.selectPlanPostById(id));
    }

    /**
     * 新增应急岗位
     */
    @PreAuthorize(hasPermi = "business/plan:planPost:add")
    @Log(title = "应急岗位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PlanPost planPost)
    {
         if(planPostService.checkName(planPost)!=0){
            return AjaxResult.error("新增失败，名称重复");
        }
        return toAjax(planPostService.insertPlanPost(planPost));
    }

    /**
     * 修改应急岗位
     */
    @PreAuthorize(hasPermi = "business/plan:planPost:edit")
    @Log(title = "应急岗位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PlanPost planPost)
    {
         if(planPostService.checkName(planPost)==1){
            return AjaxResult.error("修改失败，名称重复");
        }

        return toAjax(planPostService.updatePlanPost(planPost));
    }

    /**
     * 删除应急岗位
     */
    @PreAuthorize(hasPermi = "business/plan:planPost:remove")
    @Log(title = "应急岗位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(planPostService.deletePlanPostByIds(ids));
    }
}
